package com.draven.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.draven.annotation.Log;
import com.draven.common.Result;
import com.draven.entity.Bookmark;
import com.draven.service.BookmarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 网络地址控制器
 */
@RestController
@RequestMapping("/bookmark")
public class BookmarkController {

    @Autowired
    private BookmarkService bookmarkService;

    /**
     * 添加网络地址
     */
    @PostMapping("/add")
    @Log(module = "书签管理", operationType = "CREATE", description = "添加书签")
    public Result<Long> add(
            HttpServletRequest request,
            @NotBlank(message = "标题不能为空") @RequestParam String title,
            @NotBlank(message = "URL地址不能为空") @RequestParam String url,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Integer sort,
            @RequestParam(required = false) Integer isFavorite) {
        Long userId = (Long) request.getAttribute("userId");
        Long id = bookmarkService.add(userId, title, url, category, sort, isFavorite);
        return Result.success(id);
    }

    /**
     * 修改网络地址
     */
    @PostMapping("/update")
    @Log(module = "书签管理", operationType = "UPDATE", description = "修改书签")
    public Result<Void> update(
            HttpServletRequest request,
            @NotNull(message = "ID不能为空") @RequestParam Long id,
            @NotBlank(message = "标题不能为空") @RequestParam String title,
            @NotBlank(message = "URL地址不能为空") @RequestParam String url,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Integer sort,
            @RequestParam(required = false) Integer isFavorite) {
        Long userId = (Long) request.getAttribute("userId");
        bookmarkService.update(id, userId, title, url, category, sort, isFavorite);
        return Result.success();
    }

    /**
     * 删除网络地址
     */
    @PostMapping("/delete")
    @Log(module = "书签管理", operationType = "DELETE", description = "删除书签")
    public Result<Void> delete(
            HttpServletRequest request,
            @NotNull(message = "ID不能为空") @RequestParam Long id) {
        Long userId = (Long) request.getAttribute("userId");
        bookmarkService.delete(id, userId);
        return Result.success();
    }

    /**
     * 获取网络地址详情
     */
    @GetMapping("/detail")
    public Result<Bookmark> detail(
            HttpServletRequest request,
            @NotNull(message = "ID不能为空") @RequestParam Long id) {
        Long userId = (Long) request.getAttribute("userId");
        Bookmark detail = bookmarkService.getDetail(id, userId);
        return Result.success(detail);
    }

    /**
     * 分页查询网络地址
     */
    @GetMapping("/page")
    public Result<Page<Bookmark>> page(
            HttpServletRequest request,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String url,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Integer isFavorite,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Long userId = (Long) request.getAttribute("userId");
        Page<Bookmark> page = bookmarkService.page(userId, title, url, category, isFavorite, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 切换收藏状态
     */
    @PostMapping("/toggleFavorite")
    @Log(module = "书签管理", operationType = "UPDATE", description = "切换收藏状态")
    public Result<Integer> toggleFavorite(
            HttpServletRequest request,
            @NotNull(message = "ID不能为空") @RequestParam Long id) {
        Long userId = (Long) request.getAttribute("userId");
        Integer isFavorite = bookmarkService.toggleFavorite(id, userId);
        return Result.success(isFavorite);
    }

    /**
     * 更新排序
     */
    @PostMapping("/updateSort")
    @Log(module = "书签管理", operationType = "UPDATE", description = "更新书签排序")
    public Result<Void> updateSort(
            HttpServletRequest request,
            @NotNull(message = "ID不能为空") @RequestParam Long id,
            @NotNull(message = "排序不能为空") @RequestParam Integer sort) {
        Long userId = (Long) request.getAttribute("userId");
        bookmarkService.updateSort(id, userId, sort);
        return Result.success();
    }
} 